import javax.swing.JFrame;
import javax.swing.JOptionPane;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.DataStore;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.proto.states.MsgReceiver;

import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.*;
import jade.domain.FIPAException;

public class TourGuideAgent extends Agent {

	/**
	 * 
	 */
	
	//We need another attribute for artifacts???
	private static final long serialVersionUID = -6742212850518756913L;
	private AID fittingMuseum;
	protected void setup()
	{
		//TODO Think about how to get the information about Artifacts, (OneShotBehaviour or Setup)
		
		//First of all we search all museum to find the one for what the tourguidAgent should build tours
		
		//The museum is defined by the first argument
		Object[] args = getArguments();
		
		if (args!= null && args.length == 1)
		{
			//This method searches for the museum in the registered museums
			this.findMuseum((String)args[0]);
		}
		else
		{	
			JOptionPane.showMessageDialog(new JFrame(),
				    "Number of Arguments was wrong, think of defining the museum as an argument. Agent terminates.",
				    "Museum error",
				    JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		}
		
		DFAgentDescription dfd = new DFAgentDescription();
		dfd.setName( getAID() );
		ServiceDescription sd = new ServiceDescription();
		sd.setType( "TourGuide" );
		sd.setName( getLocalName() );
		dfd.addServices(sd);
		
		try
		{
			DFService.register(this, dfd);
		}
		catch ( FIPAException fe)
		{
			fe.printStackTrace();
		}
		this.addBehaviour(new ReceiveTourQuestions(this, MessageTemplate.MatchContent("SendTour")));
	}
	
	/**
	 * This method search in all registered Museum for the one with the name, if no such a museum is registered the agent terminates
	 * @param name
	 */
	void findMuseum(String name)
	{
		System.out.println("Name to find: "+name);
		DFAgentDescription template = new DFAgentDescription();
		ServiceDescription sd = new ServiceDescription();
		sd.setType("Museum");
		template.addServices(sd);
		try 
		{
			//Get all registered Museums
			DFAgentDescription[] result = DFService.search(this, template);
			if (result.length > 0)
			{
				for (int i = 0; i < result.length; i++)
				{
					System.out.println(result[i].getName().getLocalName());
					System.out.println(result[i].getName().getLocalName().toString().equals(name));
					if (result[i].getName().getLocalName().toString().equals(name))
					{
						this.fittingMuseum = result[i].getName();
						break;
					}
				}
				if (this.fittingMuseum == null)
				{
					JOptionPane.showMessageDialog(new JFrame(), "No proper museum/gallery exists. Agent terminates.", "Museum error", JOptionPane.ERROR_MESSAGE);
					System.exit(0);
				}
				
			}
		} 
		catch (FIPAException e) {e.printStackTrace();}
	}
	
	class ReceiveTourQuestions extends MsgReceiver
	{
		public ReceiveTourQuestions( Agent a, MessageTemplate mt ){ super(a, mt, MsgReceiver.INFINITE, new DataStore(), "Key"); }
		
		protected void handleMessage(ACLMessage msg)
		{
			//This method should create a random (that should be intelligent enough for the task) selection of Artifacts and send them back to sender
			AID sender = msg.getSender();
		}
	}
	
	class GetAllItems extends OneShotBehaviour
	{
		public GetAllItems (Agent a) 
		{
			super(a);
		}

		@Override
		public void action() {
			// TODO Send a message to the museum and ask for infomration about all artifacts
			
		}
	}
	
}
